<template>
	<view>
		<view v-if='isInviteCaptain' class="content_box">
			<view class="first-top">
				<view class="first-banner">
					<view class="banner-box" @click="go('team/member')">
						<img src="@/static/images/teams.png" />
						<text>团队成员</text>
					</view>
					<view class="banner-box" @click="go('team/achievement')">
						<img src="@/static/images/team_together.png" />
						<text>团队战绩</text>
					</view>

	<!-- 				<view class="banner-box" @click="go('home/invite?index=captain')" v-if="isInviteRegion">
						<img src="@/static/images/invaite.png" />
						<text>邀请队长</text>
					</view> -->

					<view class="banner-box" @click="go('home/invite?index=member')" v-if="isInviteCaptain">
						<img src="@/static/images/honer.png" />
						<text>邀请队员</text>
					</view>
				</view>

				<view class="second-banner">
					<view class="second-item">
						<view class="item-num">
							<text>{{ statis.people}}</text>
						</view>
						<view class="item-name">
							<text>团队人数</text>
						</view>
					</view>
					<view class="second-item">
						<view class="item-num is-border">
							<text>{{ statis.query}}</text>
						</view>
						<view class="item-name">
							<text>累计单数</text>
						</view>
					</view>
					<view class="second-item">
						<view class="item-num">
							<text>{{zeroTwo(statis.income)}}</text>
						</view>
						<view class="item-name">
							<text>团队总收入</text>
						</view>
					</view>
				</view>
			</view>

			<view class="ad">
				<view class="ad-text">
					<view class="marqueeText" :style="{ transform: `translateX(${position}px)` }">{{msg}}
					</view>
				</view>
			</view>

			<view class="revenue-box">
				<view class="revenue-box-money">
					<image src="@/static/images/inner_icon.png"></image>
					<text>团队带给您的收入</text>
				</view>
				<view :loading="loading">
					<view v-for="(item, index) of tableData" class="revenue-box-item">
						<view>
							<view class="right-top">
								<text>{{item.nickName}}</text>
							</view>
							<view class="right-bottom">
								<text>{{item.keyword}}</text>
							</view>
						</view>
						<view class="revenue-box-left">
							<view class="left-top">
								<text>{{ zeroTwo(item.amount)}}</text>
							</view>
							<view class="left-botom">
								<text>{{ item.createTime}}</text>
							</view>
						</view>
					</view>
					<view v-if="!tableData.length" style="text-align: center;">暂无</view>
				</view>
			</view>
		</view>
		<view v-else id="hb">
			<image src="@/static/images/teamPoster.jpg" mode="widthFix" style="width: 100%;"></image>
			<view v-if='!$auth.hasRole("group")' id="fd" @click="$tab.navigateTo('/pages/mine/upgrade')">去升级组建团队</view>
		</view>
	</view>
</template>

<script>
	import {
		getValue
	} from '@/api/index.js'

	import {
		teamStatistics,
		teamIncome
	} from '@/api/team.js'

	export default {
		data() {
			return {
				text: "签小查最新版本正式上线! ",
				position: 0,
				animationFrame: null,

				statis: {
					income: 0,
					people: 0,
					query: 0
				},
				msg: '欢迎注册使用！',

				loading: true,
				tableData: []
			}
		},
		computed: {
			isInviteCaptain() {
				return this.isLogin() && this.$auth.hasRoleOr(["region", "captain"])
			},
			isInviteRegion() {
				return this.isLogin() && this.$auth.hasRoleOr(["region"])
			}
		},
		mounted() {
			let is = this.isLogin() && this.$auth.hasRoleOr(["region", "captain"])
			if (is) {
				getValue('groupMsg').then(res => {
					this.msg = res
				})
				teamStatistics().then(res => {
					this.statis = res
				})
				teamIncome().then(res => {
					this.tableData = res.data
					// console.log('--this.tableData--', this.tableData)
					this.loading = false
				})
				this.startMarquee();
			}
		},
		beforeDestroy() {
			cancelAnimationFrame(this.animationFrame);
		},
		methods: {
			startMarquee() {
				let containerWidth = 0
				let contentWidth = 0 // 因为复制了一份内容
				const query = wx.createSelectorQuery()

				// 获取view宽度
				query.select('.ad-text').boundingClientRect(rect => {
					containerWidth = rect ? rect.width : 0
				})

				// 获取text宽度
				query.select('.marqueeText').boundingClientRect(rect => {
					contentWidth = rect ? rect.width : 0
				})
				query.exec()
				const animate = () => {
					this.position -= 1;
					if (this.position < -contentWidth) {
						this.position = containerWidth;
					}
					this.animationFrame = requestAnimationFrame(animate);
				};
				this.animationFrame = requestAnimationFrame(animate);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: linear-gradient(180deg, #c4d6ff, #f7f8fa);
		min-height: 100%;
	}

	.content_box {
		width: 100%;
		height: auto;
		padding: 0;
		margin: 0;
		padding-bottom: 25vw;

		.first-top {
			width: 100%;
			height: auto;
			background-image: url(@/static/images/index/banner_.png);
			background-size: 100% auto;
			background-repeat: no-repeat;

			.first-banner {
				padding-top: 10vw;
				display: flex;
				justify-content: space-around;

				.banner-box {
					width: 18%;
					height: auto;

					border-radius: 2vw;
					background-size: 100% 100%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;

					img {
						width: 100%;
						height: auto;
					}

					text {
						font-size: 3.2vw;
						font-weight: bolder;
					}
				}
			}

			.second-banner {
				// width: 96%;
				// padding: 2vw;
				// margin: 0 auto;
				// border-radius: 2vw;

				width: 96%;
				padding: 2vw;
				margin: 0 auto;
				border-radius: 2vw;		
				background-color: #fff;
				
				
				background-image: url(@/static/images/total_banner.png);
				background-size: 100% 100%;
				background-repeat: no-repeat;
				margin-top: 4vw;

				display: flex;
				justify-content: center;
				align-items: center;

				.second-item {
					width: 30%;
					height: auto;
					text-align: center;
					font-size: 3.2vw;
					font-weight: bold;

					.item-num {
						font-size: 5.2vw;
						color: #097BF0;
					}

					.item-name {
						margin: 1vw 0;
					}

					.is-border {
						border: 1px solid #E2E2E2;
						border-top: none;
						border-bottom: none;
					}
				}
			}

		}
	}

	.ad {
		width: 96%;
		height: 10vw;
		font-size: 4vw;
		font-weight: 700;
		margin: 2vw auto;
		background-image: url(/static/images/over_talk.png);
		background-size: 100% 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #186EE4;

		padding-right: 2vw;
		padding-left: 16vw;

		.ad-text {
			width: 95%;
			overflow: hidden;
			white-space: nowrap;
		}
	}

	.revenue-box {
		width: 96%;
		background-color: #fff;
		padding: 2vw 3vw;
		margin: 0 auto;
		border-radius: 2vw;

		.revenue-box-money {
			width: 100%;
			height: 6vw;

			display: flex;
			align-items: center;
			gap: 2vw;
			font-size: 3.5vw;
			font-weight: 600;

			image {
				width: 5vw;
				height: 5vw;
			}
		}

		.revenue-box-item {
			width: 100%;
			height: 18vw;
			padding: 0 2vw;
			background: linear-gradient(to right, #F4F9F5, #FBF1E8);
			margin-top: 2vw;
			border-radius: 2vw;
			display: flex;
			justify-content: space-between;
			align-items: center;
			gap: 10vw;
			text-align: center;
			font-size: 3.5vw;

			.right-top {
				font-weight: 700;
				color: #333333;
			}

			.right-bottom {
				margin-top: 3vw;
				color: #666666;
			}

			.revenue-box-left {
				text-align: right;

				.left-top {
					color: #D9670F;
					font-size: 6vw;
					font-weight: 700;
				}

				.left-botom {
					margin-top: 3vw;
					color: #999999;
				}
			}
		}
	}

	#hb {
		width: 100vw;

		#fd {
			position: fixed;
			z-index: 10;
			bottom: 10%;
			left: 20%;
			width: 60%;
			border-radius: 2vw;
			padding: 10px 0;
			text-align: center;
			background-color: #fcb829;
			color: #fff;
		}
	}
</style>